package com.nicole.reqresp.filters;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import java.io.IOException;

/**
 * @author  xiaoying
 */
public class MyServletOutputStream extends ServletOutputStream {
    private ServletOutputStream outputStream;
    private StringBuffer buffer;
    public MyServletOutputStream(ServletOutputStream outputStream) {
        this.outputStream = outputStream;
        buffer = new StringBuffer();
    }
    @Override
    public void write(int b) throws IOException {
        outputStream.write(b);
    }
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        outputStream.write(b, off, len);
        byte[] bytes = new byte[len];
        System.arraycopy(b, off, bytes, 0, len);
        buffer.append(new String(bytes,"UTF-8"));
    }
    @Override
    public void write(byte[] b) throws IOException {
        outputStream.write(b);
    }
//    @Override
//    public void flush() throws IOException {
//        super.flush();
//    }
    //清空buffer，以便下一次重新使用
    public void myFlush(){
        buffer = null;
    }
    public String getBuffer() {
        if (buffer != null){
            return buffer.toString();
        }
        return null;
    }

    @Override
    public boolean isReady() {
        return false;
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {

    }
}
